package priv.lan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import priv.lan.pojo.Scenic;
import priv.lan.pojo.User;
import priv.lan.pojo.vo.ScenicQuery;
import priv.lan.pojo.vo.UserQuery;
import priv.lan.service.ScenicService;
import priv.lan.util.R;

import javax.management.Query;
import java.util.List;

/**
 * @Description: TODO
 * @author: lan
 * @date: 2021/12/3 20:15
 * @Version: 1.0
 */
@Api("ScenicController")
@CrossOrigin
@RestController
@RequestMapping("/scenic")
public class ScenicController {
    @Autowired
    ScenicService scenicService;

    @PostMapping("addScenic")
    public R addScenic(@RequestBody Scenic scenic){
       Boolean isSave = scenicService.saveOrUpdate(scenic);
        if (isSave){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("updateScenic")
    public R updateScenic(@RequestBody Scenic scenic){
        boolean b = scenicService.updateById(scenic);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }



    //条件查询带分页的方法  当前页，记录数
    @PostMapping("scenicPage/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) ScenicQuery scenicQuery) {
        //创建page对象
        Page<Scenic> scenicPage = new Page<>(current,limit);
        //构建条件
        QueryWrapper<Scenic> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = scenicQuery.getName();
        String scenicType = scenicQuery.getScenicType();
        String place = scenicQuery.getPlace();
        String begin = scenicQuery.getBegin();
        String end = scenicQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!ObjectUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!ObjectUtils.isEmpty(scenicType)) {
            wrapper.eq("scenic_type",scenicType);
        }

        if(!ObjectUtils.isEmpty(place)) {
            wrapper.like("place",place);
        }
        if(!ObjectUtils.isEmpty(begin)) {
            wrapper.ge("create_time",begin);
        }
        if(!ObjectUtils.isEmpty(end)) {
            wrapper.le("create_time",end);
        }
        //排序
        wrapper.orderByDesc("create_time");
        wrapper.eq("is_deleted", 0);
        //调用方法实现条件查询分页
        scenicService.page(scenicPage,wrapper);
        long total = scenicPage.getTotal();//总记录数
        List<Scenic> records = scenicPage.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable String id){
        Scenic byId = scenicService.getById(id);
        if (byId!=null){
            return R.ok().data("scenic", byId);
        }else {
            return R.error();
        }
    }

    @PostMapping("deleteById/{id}")
    public R deleteByid(@PathVariable String id){
//        逻辑删除 将is_deleted 变成 1
        if (scenicService.deletByLogic(id)){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("uploadImage")
    public R uploadImage(MultipartFile file){
        String url = scenicService.uploadImageToALOSS(file);
        return R.ok().data("url",url);
    }


}
